import 'package:casdon/common/utils/string_externtion.dart';
import 'package:casdon/modules/recipe/data/api/recipe_category_api.dart';
import 'package:casdon/modules/recipe/ui/widgets/recipe_category_item.dart';

class RecipeCategoryModel extends RecipeCategoryData {
  List<RecipeCategoryModel>? childrenList;
  @override
  final String title;

  @override
  final String id;

  @override
  final int nodeType;

  @override
  final int sort;

  @override
  String get imgUrl => _imgUrl.cropParams100;

  final String _imgUrl;

  RecipeCategoryModel({
    required this.childrenList,
    required this.id,
    required this.nodeType,
    required this.sort,
    required this.title,
    required String imgUrl,
  }) : _imgUrl = imgUrl;

  // 静态方法转换
  static RecipeCategoryModel fromApi(RecipeCategory response) {
    return RecipeCategoryModel(
      childrenList: RecipeCategoryModel.listFromApi(
        response.childrenList ?? [],
      ),
      id: response.id,
      nodeType: response.nodeType,
      sort: response.sort,
      title: response.name ?? "",
      imgUrl: (response.imageUrl ?? "").isEmpty
          ? "https://i.ibb.co/j9K4KJ6K/541-3x.png"
          : response.imageUrl!,
    );
  }

  // 批量转换的静态方法
  static List<RecipeCategoryModel> listFromApi(List<RecipeCategory> items) {
    return items.map(fromApi).toList();
  }

  @override
  RecipeCategoryData placeholder() {
    return RecipeCategoryModel(
      childrenList: [],
      title: "全部",
      id: "",
      nodeType: 0,
      sort: -1,
      imgUrl: "https://i.ibb.co/j9K4KJ6K/541-3x.png",
    );
  }
}
